package cn.droidlover.xdroidmvp.mvp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.R;
import cn.droidlover.xdroidmvp.XDroidConf;
import cn.droidlover.xdroidmvp.base.ActivityCollector;
import cn.droidlover.xdroidmvp.custom.SeatImgListener;
import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.kit.KnifeKit;
import cn.droidlover.xdroidmvp.custom.StringUtil;

/**
 * Created by wanglei on 2016/12/29.
 */

public abstract class XActivity<P extends IPresent> extends RxAppCompatActivity implements IView<P> {

    private VDelegate vDelegate;
    private P p;
    protected Activity context;

    private RxPermissions rxPermissions;

    private Unbinder unbinder;

    private TitleListener titleListener;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarUpperAPI21();
        ActivityCollector.addActivity(this);
        context = this;
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
            bindUI(null);
            bindEvent();
        }
        //ButterKnife.bind(this);
        initData(savedInstanceState);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setStatusBarUpperAPI21() {
        Window window = getWindow();
        //取消设置悬浮透明状态栏,ContentView便不会进入状态栏的下方了
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //设置状态栏颜色
        if(Integer.valueOf(Build.VERSION.SDK_INT) > 21){
            window.setStatusBarColor(Color.parseColor("#222222"));
        }
    }

    /**
     * 初始化导航栏(若不符合此导航结构， 请单独编写，并在此处注明）
     *
     * @param context 上下文
     * @param isGoBack  是否显示回退
     * @param title     标题文本
     * @param titleColor 标题颜色
     * @param rightText 右侧文字


     * @param listener 右侧按钮点击事件监听回调

     */
    public void initTitle(Context context, boolean isGoBack, String title, int titleColor,String rightText, TitleListener listener) {
        ImageView imgBtnGoback = (ImageView) findViewById(R.id.tv_back);
        if(listener!=null)titleListener = listener;
        if (isGoBack) {
              //  imgBtnGoback.setImageResource(GoBackImg);
            imgBtnGoback.setVisibility(View.VISIBLE);
            imgBtnGoback.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        TextView text = (TextView) findViewById(R.id.tv_title);
        if (!StringUtil.isEmpty(title)) {
            text.setVisibility(View.VISIBLE);
            text.setText(title);
            if(titleColor!=0){
                text.setTextColor(context.getResources().getColor(titleColor));
            }else{
                text.setTextColor(Color.parseColor("#f0f0f0"));
            }
                //text.setTextColor(middle_textcolor);

        }
        TextView rightClick = (TextView) findViewById(R.id.tv_right);
        if (!StringUtil.isEmpty(rightText)) {
            rightClick.setVisibility(View.VISIBLE);
            rightClick.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(titleListener!=null){
                        titleListener.rightClickListener();
                    }
                }
            });
            rightClick.setText(rightText);
        }
        //RelativeLayout layout= (RelativeLayout) findViewById(R.id.ll_title_bar_id);

    }

    private View layout;
    private ImageView iv;
    private TextView text;

    /**
     * 初始化占位图
     * @param context 上下文
     * @param isShow 是否显示占位图
     * @param textStr 占位图底部的文字
     * @param ImgRes 占位图资源
     * @param listener 点击监听
     */
    public void initSeatPic(Context context, boolean isShow, String textStr, int ImgRes, final SeatImgListener listener){
        if(layout == null){
            layout = LayoutInflater.from(context).inflate(R.layout.data_seat_pic,null);
            iv = (ImageView) layout.findViewById(R.id.iv_pic);
            text = (TextView) layout.findViewById(R.id.tv_text);
        }
        if(isShow){
            layout.setVisibility(View.VISIBLE);
            iv.setImageResource(ImgRes);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener != null){
                        listener.OnSeatClick();
                    }
                }
            });
            if(!StringUtil.isEmpty(textStr)){
                text.setText(textStr);
            }
        }else{
            layout.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏占位图
     * @param context 上下文
     */
    public void initSeatPic(Context context){
        if(layout == null){
            layout = LayoutInflater.from(context).inflate(R.layout.data_seat_pic,null);
            iv = (ImageView) layout.findViewById(R.id.iv_pic);
            text = (TextView) layout.findViewById(R.id.tv_text);
        }
        layout.setVisibility(View.GONE);
    }

    @Override
    public void bindUI(View rootView) {
        unbinder = KnifeKit.bind(this);
    }

    protected VDelegate getvDelegate() {
        if (vDelegate == null) {
            vDelegate = VDelegateBase.create(context);
        }
        return vDelegate;
    }

    protected P getP() {
        if (p == null) {
            p = newP();
            if (p != null) {
                p.attachV(this);
            }
        }
        return p;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (useEventBus()) {
            BusProvider.getBus().register(this);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        getvDelegate().resume();
    }


    @Override
    protected void onPause() {
        super.onPause();
        getvDelegate().pause();
    }

    @Override
    public boolean useEventBus() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
        if (useEventBus()) {
            BusProvider.getBus().unregister(this);
        }
        if (getP() != null) {
            getP().detachV();
        }
        getvDelegate().destory();
        p = null;
        vDelegate = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (getOptionsMenuId() > 0) {
            getMenuInflater().inflate(getOptionsMenuId(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    protected RxPermissions getRxPermissions() {
        rxPermissions = new RxPermissions(this);
        rxPermissions.setLogging(XDroidConf.DEV);
        return rxPermissions;
    }

    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    @Override
    public void bindEvent() {

    }

    @Override
     public Resources getResources() {
         Resources resources = super.getResources();
         Configuration configuration = new Configuration();
         configuration.setToDefaults();
         resources.updateConfiguration(configuration, resources.getDisplayMetrics());
         return resources;
     }
}
